package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

type UserInfo struct {
	UserName string `form:"userName" json:"userName" binding:"required"`
	Password string `form:"password" json:"password" binding:"required"`
}

func main() {
	engine := gin.Default()
	engine.GET("/login", func(context *gin.Context) {
		var userInfo UserInfo
		err := context.ShouldBind(&userInfo)
		if err == nil {
			fmt.Printf("login info:%#v\n", userInfo)
			context.JSON(http.StatusOK, gin.H{
				"userName": userInfo.UserName,
				"password": userInfo.Password,
			})
		} else {
			context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		}
	})

	engine.POST("/login", func(context *gin.Context) {
		var userInfo UserInfo
		err := context.ShouldBind(&userInfo)
		if err == nil {
			fmt.Printf("login info:%#v\n", userInfo)
			context.JSON(http.StatusOK, gin.H{
				"userName": userInfo.UserName,
				"password": userInfo.Password,
			})
		} else {
			context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		}
	})

	engine.Run(":8088")
}
